<template>
  <div>
    <div>【数据传递信息】</div>
    <div ref="relationRef" class="w-full h-full"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  data: {
    type: Object,
    required: true
  }
})

const relationRef = ref(null)
let myChart: any = null
onMounted(() => {
  myChart = echarts.init(relationRef.value)
  renderChart()
})


const renderChart = () => {
  let option = {
    xAxis: {
      show: false,
      type: 'value'
    },
    yAxis: {
      show: false,
      type: 'value'
    },
    series: [
      {
        type: 'graph',
        layout: 'none',
        coordinateSystem: 'cartesian2d',
        symbolSize: 26,
        z: 3,
        // 关系线条上的文字
        edgeLabel: {
          show: true,
          color: '#fff',
          fontSize: 14,
          formatter: function(parms: any) {
            return parms.data.speed || ''
          }
        },
        // 图形下的文本标签
        label: {
          show: true,
          color: '#5e5e5e',
          position: 'bottom',
        },
        // 边两端的标记类型
        edgeSymbol: ['none', 'arrow'],
        // 边两端的标记大小
        edgeSymbolSize: 8,
        data: props.data.relations.map((item: any) => {
          // 数据中心
          if (item.id === 0) {
            return {
              name: item.name,
              value: item.value,
              symbolSize: 100,
              itemStyle: {
                color: {
                  colorStops: [
                    { offset: 0, color: '#157eff' },
                    { offset: 1, color: '#35c2ff' },
                  ]
                }
              },
              label: {
                fontSize: '14'
              }
            }
          }
          // 非数据中心（子平台）
          else {
            return {
              name: item.name,
              value: item.value,
              speed: `${item.speed}kb/s`
            }
          }
        }),
        // 节点间的关系数据
        links: props.data.relations.map((item: any) => ({
          source: item.source,
          target: item.target,
          speed: `${item.speed}kb/s`,
          lineStyle: {
            color: '#12b5d0',
            curveness: 0.2
          },
          label: {
            show: true,
            position: 'middle',

          }
        }))
      },
      {
        type: 'lines',
        coordinateSystem: 'cartesian2d',
        z: 1,
        effect: {
          show: true,
          trailLength: 0,
          symbol: 'arrow',
          color: 'rgba(55,155,255,0.5)',
          symbolSize: 12
        },
        lineStyle: {
          curveness: 0.2
        },
        data: [
          [{ coord: [0, 300] }, { coord: [50, 200] }],
          [{ coord: [0, 100] }, { coord: [50, 200] }],
          [{ coord: [50, 200] }, { coord: [100, 100] }],
          [{ coord: [50, 200] }, { coord: [100, 300] }]
        ]
      }
    ]

  }
  myChart.setOption(option)
}

watch(() => props.data, () => {
  renderChart()
})
</script>

<style scoped>
</style>
